package biz.obake.team.touchprotector.lfd;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;

public class c implements a {

    /* renamed from: a  reason: collision with root package name */
    private Context f1733a;

    /* renamed from: b  reason: collision with root package name */
    private String f1734b;

    /* renamed from: c  reason: collision with root package name */
    private Resources f1735c;

    /* renamed from: d  reason: collision with root package name */
    private List<String> f1736d;
    private String[] e;
    private int f;

    public class b extends Exception {
        private b(c cVar, String str) {
            super(str);
        }

        private b(c cVar, String str, Object... objArr) {
            super(String.format(str, objArr));
        }
    }

    public c(String str) {
        o(str);
    }

    private void d() {
        if (this.f1736d == null) {
            int identifier = this.f1735c.getIdentifier("epngjxvz_designs", "array", this.f1734b);
            if (identifier != 0) {
                TypedArray obtainTypedArray = this.f1735c.obtainTypedArray(identifier);
                ArrayList arrayList = new ArrayList();
                for (int i = 0; i < obtainTypedArray.length(); i++) {
                    arrayList.add(this.f1735c.getResourceEntryName(obtainTypedArray.getResourceId(i, 0)));
                }
                obtainTypedArray.recycle();
                this.f1736d = arrayList;
                return;
            }
            throw new b("resource not found: %s", new Object[]{"images"});
        }
    }

    private void e() {
        if (this.e == null) {
            int identifier = this.f1735c.getIdentifier("epngjxvz_titles", "array", this.f1734b);
            if (identifier != 0) {
                this.e = this.f1735c.getStringArray(identifier);
            } else {
                throw new b("resource not found: %s", new Object[]{"images"});
            }
        }
    }

    private Drawable f(String str) {
        int identifier = this.f1735c.getIdentifier(str, "drawable", this.f1734b);
        if (identifier != 0) {
            return this.f1735c.getDrawable(identifier).mutate();
        }
        throw new b("resource not found: %s", new Object[]{str});
    }

    private String l(String str) {
        int identifier = this.f1735c.getIdentifier(str, "string", this.f1734b);
        if (identifier != 0) {
            return this.f1735c.getString(identifier);
        }
        throw new b("resource not found: %s", new Object[]{str});
    }

    private void o(String str) {
        try {
            this.f1734b = str;
            biz.obake.team.android.a a2 = biz.obake.team.android.a.a();
            PackageInfo packageInfo = a2.getPackageManager().getPackageInfo(this.f1734b, 64);
            if ((packageInfo.applicationInfo.flags & 1) == 0) {
                Context createPackageContext = a2.createPackageContext(str, 0);
                this.f1733a = createPackageContext;
                this.f1735c = createPackageContext.getResources();
                JSONObject p = p();
                String string = p.getString("type");
                if ("epngjxvz".equals(string)) {
                    int i = p.getInt("version");
                    if (1 < i) {
                        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=biz.obake.team.touchprotector"));
                        intent.setFlags(268435456);
                        biz.obake.team.android.a.a().startActivity(intent);
                        throw new b("invalid version: %d", new Object[]{Integer.valueOf(i)});
                    }
                    if ((packageInfo.applicationInfo.flags & 2) == 0) {
                        String string2 = p.getString("package_name");
                        if (!this.f1734b.equals(string2)) {
                            throw new b("invalid package_name: %s", new Object[]{string2});
                        } else if (packageInfo.signatures.length == 1) {
                            if (!p.getString("x509_sha256").equals(biz.obake.team.touchprotector.c.d(MessageDigest.getInstance("SHA256").digest(packageInfo.signatures[0].toByteArray())))) {
                                throw new b("signature unmatched: %s", new Object[]{string2});
                            }
                        } else {
                            throw new b("multiple signatures: %s", new Object[]{string2});
                        }
                    }
                    this.f = p.getInt("max_designs");
                    return;
                }
                throw new b("invalid type: %s", new Object[]{string});
            }
            throw new b("system package: %s", new Object[]{this.f1734b});
        } catch (PackageManager.NameNotFoundException | Resources.NotFoundException | IOException | InvalidKeyException | NoSuchAlgorithmException | SignatureException | CertificateException | JSONException unused) {
            throw new b("init failed: %s", new Object[]{this.f1734b});
        }
    }

    private JSONObject p() {
        InputStream inputStream = null;
        try {
            inputStream = this.f1733a.getAssets().open("epngjxvz");
            byte[] b2 = biz.obake.team.android.c.b(inputStream, 256);
            byte[] b3 = biz.obake.team.android.c.b(inputStream, 16384);
            q(b3, b2);
            return new JSONObject(new String(b3, "UTF-8"));
        } finally {
            biz.obake.team.android.c.a(inputStream);
        }
    }

    private void q(byte[] bArr, byte[] bArr2) {
        InputStream inputStream = null;
        try {
            InputStream open = biz.obake.team.android.a.a().getAssets().open("epngjxvz.cer");
            try {
                PublicKey publicKey = ((X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(open)).getPublicKey();
                Signature instance = Signature.getInstance("SHA256withRSA");
                instance.initVerify(publicKey);
                instance.update(bArr);
                if (instance.verify(bArr2)) {
                    biz.obake.team.android.c.a(open);
                    return;
                }
                throw new b("invalid signature");
            } catch (Throwable th) {
                th = th;
                inputStream = open;
                biz.obake.team.android.c.a(inputStream);
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
            biz.obake.team.android.c.a(inputStream);
            throw th;
        }
    }

    public String a(int i) {
        try {
            String g = g(i);
            return this.f1734b + ":" + g;
        } catch (b unused) {
            return "preset:1";
        }
    }

    public Drawable b(int i) {
        try {
            return f(g(i));
        } catch (b unused) {
            return null;
        }
    }

    public String c(int i) {
        try {
            e();
            return this.e[i];
        } catch (b unused) {
            return null;
        }
    }

    public String g(int i) {
        d();
        return this.f1736d.get(i);
    }

    public int getCount() {
        try {
            d();
            return Math.min(this.f1736d.size(), this.f);
        } catch (b unused) {
            return 0;
        }
    }

    public Drawable h() {
        return f("epngjxvz_icon");
    }

    public int i(String str) {
        d();
        return this.f1736d.indexOf(str);
    }

    public String j() {
        return this.f1734b;
    }

    public String k() {
        return l("epngjxvz_provider");
    }

    public String m() {
        return l("epngjxvz_summary");
    }

    public String n() {
        return l("epngjxvz_title");
    }
}
